﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_BGM_MAS_Source : IDAL_BGM_MAS_Source
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();

        public IList<M_BGM_MAS_Source> GetFull(bool bLeafOnly)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_MAS_Source> infoList = new List<M_BGM_MAS_Source>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT s.bgm_source_seq, s.bgm_source_type_seq, st.bgm_source_type_name"
                    + ", s.bgm_source_name, s.upper_bgm_source_seq"
                    + ", us.bgm_source_name as upper_bgm_source_name"
                    + ", s.bgm_year_type_seq, yt.bgm_year_type_name"
                    + ", yt.day_start, yt.month_start"
                    + ", s.record_status"
                    + " FROM bgm_mas_source s"
                    + " LEFT JOIN bgm_mas_source_type st ON s.bgm_source_type_seq = st.bgm_source_type_seq"
                    + " LEFT JOIN bgm_mas_year_type yt ON s.bgm_year_type_seq = yt.bgm_year_type_seq"
                    + " LEFT JOIN bgm_mas_source us ON s.upper_bgm_source_seq = us.bgm_source_seq"
                    + " LEFT JOIN bgm_mas_source ls ON s.bgm_source_seq = ls.upper_bgm_source_seq"
                        + " AND ls.record_status not like @delete_status"
                    + " WHERE s.record_status not like @delete_status"
                    + (bLeafOnly ? " and ls.bgm_source_seq is null" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_MAS_Source info = new M_BGM_MAS_Source();
                        info.bgm_source_seq = Convert.ToInt32(drc[i]["bgm_source_seq"]);
                        info.bgm_source_name = drc[i]["bgm_source_name"].ToString();
                        info.bgm_source_type_seq = cn.ConvertNullInt(drc[i]["bgm_source_type_seq"]);
                        info.bgm_source_type_name = drc[i]["bgm_source_type_name"].ToString();
                        info.upper_bgm_source_seq = cn.ConvertNullInt(drc[i]["upper_bgm_source_seq"]);
                        info.upper_bgm_source_name = drc[i]["upper_bgm_source_name"].ToString();
                        info.bgm_year_type_seq = cn.ConvertNullInt(drc[i]["bgm_year_type_seq"]);
                        info.bgm_year_type_name = drc[i]["bgm_year_type_name"].ToString();
                        info.day_start = cn.ConvertNullInt(drc[i]["day_start"]);
                        info.month_start = cn.ConvertNullInt(drc[i]["month_start"]);
                        info.record_status = drc[i]["record_status"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_BGM_MAS_Source> GetFilterList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_MAS_Source> infoList = new List<M_BGM_MAS_Source>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT s.bgm_source_seq, s.bgm_source_name, s.upper_bgm_source_seq"
                    + ", us.bgm_source_name as upper_bgm_source_name, s.record_status"
                    + " FROM bgm_mas_source s"
                    + " LEFT JOIN bgm_mas_source us ON s.upper_bgm_source_seq = us.bgm_source_seq"
                    + " LEFT JOIN bgm_mas_source ls ON s.bgm_source_seq = ls.upper_bgm_source_seq"
                        + " AND ls.record_status not like @delete_status"
                    + " WHERE s.record_status not like @delete_status"
                    + " and ls.bgm_source_seq is null";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_MAS_Source info = new M_BGM_MAS_Source();
                        info.bgm_source_seq = Convert.ToInt32(drc[i]["bgm_source_seq"]);
                        info.bgm_source_name = drc[i]["bgm_source_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_BGM_MAS_Source GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_BGM_MAS_Source info = new M_BGM_MAS_Source();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@seq", seq);

                strSQL = "SELECT s.bgm_source_seq, s.bgm_source_type_seq, st.bgm_source_type_name"
                    + ", s.bgm_source_name, s.upper_bgm_source_seq"
                    + ", us.bgm_source_name as upper_bgm_source_name"
                    + ", s.bgm_year_type_seq, yt.bgm_year_type_name"
                    + ", yt.day_start, yt.month_start"
                    + ", s.record_status"
                    + " FROM bgm_mas_source s"
                    + " LEFT JOIN bgm_mas_source_type st ON s.bgm_source_type_seq = st.bgm_source_type_seq"
                    + " LEFT JOIN bgm_mas_year_type yt ON s.bgm_year_type_seq = yt.bgm_year_type_seq"
                    + " LEFT JOIN bgm_mas_source us ON s.upper_bgm_source_seq = us.bgm_source_seq"
                    + " WHERE s.bgm_source_seq = @seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.bgm_source_seq = Convert.ToInt32(drc[0]["bgm_source_seq"]);
                    info.bgm_source_name = drc[0]["bgm_source_name"].ToString();
                    info.bgm_source_type_seq = cn.ConvertNullInt(drc[0]["bgm_source_type_seq"]);
                    info.bgm_source_type_name = drc[0]["bgm_source_type_name"].ToString();
                    info.upper_bgm_source_seq = cn.ConvertNullInt(drc[0]["upper_bgm_source_seq"]);
                    info.upper_bgm_source_name = drc[0]["upper_bgm_source_name"].ToString();
                    info.bgm_year_type_seq = cn.ConvertNullInt(drc[0]["bgm_year_type_seq"]);
                    info.bgm_year_type_name = drc[0]["bgm_year_type_name"].ToString();
                    info.day_start = cn.ConvertNullInt(drc[0]["day_start"]);
                    info.month_start = cn.ConvertNullInt(drc[0]["month_start"]);
                    info.record_status = drc[0]["record_status"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
    }
}
